package com.spring.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;


@Configuration
/* 加载了jdbc.properties*/
@PropertySource("classpath:jdbc.properties")
/*注解扫描*/
@ComponentScan("com.spring")
public class SpringConfig {
    @Value("${jdbc.url}")
    private  String url;

    @Value("${jdbc.username}")
    private  String username;

    @Value("${jdbc.password}")
    private  String password;

    @Value("${jdbc.driver}")
    private  String driver;

    /*datasource*/
    @Bean
    public DataSource dataSource(){
          DruidDataSource druidDataSource = new DruidDataSource();
          druidDataSource.setUrl(url);
          druidDataSource.setUsername(username);
          druidDataSource.setPassword(password);
          druidDataSource.setDriverClassName(driver);
          return druidDataSource;
      }

    /*qureyRunner*/
    @Bean
    public QueryRunner queryRunner(@Autowired DataSource dataSource){
        return new QueryRunner(dataSource);
    }

}

